﻿$(function () {
    var pDoc = $(parent.document);
    var $relationList = pDoc.find('.js-relation tbody'), nodeId = $('.js-nodeId').val();
    $('.js-search select[name="prototypeId"]').change(function () {
        location.href = "/m/node/nodeRelation/" + $('.js-nodeId').val() + "?prototypeId=" + $(this).val();
    });
    $('.js-search select[name="categoryId"]').change(function () {
        var $select = $('.js-search select[name="prototypeId"] option:selected');
        if ($select.val() != 0) {
            location.href = "/m/node/nodeRelation/" + $('.js-nodeId').val() + "?prototypeId=" + $select.val() + "&categoryId=" + $(this).val();
        } else {
            location.href = "/m/node/nodeRelation/" + $('.js-nodeId').val() + "?categoryId=" + $(this).val();
        }

    });
    $('.js-addrel').click(function () {
        var $this = $(this);
        var pTr = $(this).parents('tr');
        pTr.fadeOut("slow", function () {
            $.post($this.attr("href"), { id: nodeId, refId: $this.attr("itemId") }, function (data) {
                if (data) {
                    $relationList.append(pTr);
                    $this.text("删除");
                    $this.attr("href", "/m/node/delRelation");
                    $relationList.find(pTr).fadeIn("slow");
                }
            });
        });
        return false;
    });
    $('.js_batchAdd').click(function () {
        var $this = $(this);
        var $elements = $this.parents("[data-js='tabellist_style1']").find("td .checkbox_own input[type='checkbox']:checked");
        if ($elements.length == 0) {
            alert("请至少选择一条要添加关联的对象！");
        } else {
            var ids = new Array();
            $elements.each(function () {
                ids.push($(this).val());
                $(this).parents('tr').fadeOut("slow", function () {
                    $(this).remove();
                    $relationList.append($(this));
                    $(this).find('.js-addrel').text("删除");
                    $(this).find('.js-addrel').attr("href", "/m/node/delRelation");
                    $relationList.find($(this)).fadeIn("slow");
                });
            });
            $.post($this.attr("href"), { id: nodeId, refIds: ids.join(',') }, function (data) {
                if (data) {
                   
                }
            });
        }
        return false;
    });
    pDoc.find('.js-delRel').live("click", function () {
        var $this = $(this);
        $.post($(this).attr("href"), { id: nodeId, refId: $this.attr("itemId") }, function (data) {
            if (data) {
                $this.parents('tr').fadeOut("slow", function () {
                    $(this).remove();
                    location.reload();
                });
            }
        });
        return false;
    });
    pDoc.find('.js_batchDelRel').click(function () {
        var $this = $(this);
        var $elements = $this.parents("[data-js='tabellist_style1']").find("td .checkbox_own input[type='checkbox']:checked");
        if ($elements.length == 0) {
            alert("请至少选择一条要删除关联的对象！");
        } else {
            var ids = new Array();
            $elements.each(function () {
                ids.push($(this).val());
            });
            $.post($this.attr("href"), { id: nodeId, refIds: ids.join(',') }, function (data) {
                if (data) {
                    $elements.each(function () {
                        $(this).parents('tr').fadeOut("slow");
                    });
                    setTimeout("location.reload()", 500);
                }
            });
        }
        return false;
    });
});